package com.norma.macaron.service.common.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * @author zhangchaofeng
 * @date 2020/6/10 0010
 * @description MybatisPlus 代码生成器
 */
public class MybatisPlusGenerator {
    public static void main(String[] args) {

        //1、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "\\macaron-common-service\\src\\main\\java")//文件输出路径
                .setAuthor("zhangchaofeng") //作者
                .setOpen(false) //生成完成是否打开文件夹
                .setFileOverride(true) //是否覆写
                .setIdType(IdType.AUTO) //主键策略
                .setEnableCache(false) //是否启用缓存
                .setServiceName("%sService") //生成service前缀不带I
                .setBaseResultMap(true) //生成result 映射
                .setSwagger2(true) //是否启动swagger
                .setBaseColumnList(true); //生成公众的sql字段

        //2、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL)
                .setDriverName("com.mysql.jdbc.Driver")
                .setUrl("jdbc:mysql://172.16.20.27:3306/user")
                .setUsername("root")
                .setPassword("root");

        //3、策略配置
        StrategyConfig sc = new StrategyConfig();
        sc.setCapitalMode(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setEntityLombokModel(true)
                //.setTablePrefix("t_")
                .setRestControllerStyle(true);

        //4、包名策略配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.norma.macaron.service.common.mybatis")
                .setMapper("dao")
                .setService("service")
                .setController("controller")
                .setEntity("entity")
                .setXml("mapper");

        //5、 整合配置
        AutoGenerator ag = new AutoGenerator();
        ag.setGlobalConfig(gc)
                .setDataSource(dsc)
                .setStrategy(sc)
                .setPackageInfo(pc)
                .execute();
    }

}
